#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2/10/16 base trans. wrote by srcdog on 20th, April, 2009
# ld elements in base 2, 10, 16.

import os,sys

ispFile = open('isp.txt')
ispList = ispFile.readlines()
ispFile.close()

summaryFile = open('summary.txt','w')

sitelistFile = open('site.txt')
siteList = sitelistFile.readlines()
sitelistFile.close()

ErrorList = []
for line in siteList:
    if cmp('',line.strip()) == 0 :
        continue
    else:
        mainDomain = line.strip()
        domainMark = (line.strip())[::-1]


    print 'domainMark:' + domainMark
    subDomainFile = open( 'site/' + mainDomain + '.txt','w')

    delMark = 0
    start = -1
    end = -1
    for rec in ispList:
        infos = rec.split('\t')
        if len(rec.strip()) == 0 or len(infos) == 1 or rec.find('Error Line') == 0:
            ErrorList.append(rec)
            delMark += 1
            continue
        
        thisDomain = infos[0][::-1]
        if thisDomain.find(domainMark) > -1  :
            subDomainFile.write(rec)
            if start == -1 : 
                start = delMark
            else:
                end = delMark
        else :
            if start != -1 : 
                end = delMark
                break

        delMark += 1

    subDomainFile.close()
    print 'start:' + str(start) + '  end:' + str(end)
    ispList = ispList[:start] + ispList[end:]
    print len(ispList)

notFoundFile = open( 'site/notFound.txt','w')
notFoundFile.writelines(ispList)
notFoundFile.writelines(ErrorList)
notFoundFile.close()


for line in siteList:
    if cmp('',line.strip()) == 0 :
        continue
    else:
        mainDomain = line.strip()
    
    subDomainFile = open( 'site/' + mainDomain + '.txt')
    domainList = subDomainFile.readlines()
    subDomainFile.close()
    cmccCount = 0
    notCmccCount = 0
    errCount = 0
    allCount = 0

    for domain in domainList :
        allCount += 1
        if domain.find('移动') > -1 :
            cmccCount += 1
        elif domain.find('外网') > -1 :
            notCmccCount += 1
        else :
            errCount += 1

    strTemp = mainDomain+'\t'+str(cmccCount)+'\t'+str(notCmccCount)+'\t'+str(allCount)+'\t'+str(errCount)
    print strTemp
    summaryFile.write(strTemp + '\n')


summaryFile.close()



##prevMainDomain = siteList[0].strip()
#cmccCount = 0
#noCmccCount = 0
#
#for prevMainDomain in siteList:
#    prevMainDomain = prevMainDomain.strip()
#    print prevMainDomain
#
#    i = 0
#    for rec in ispList:
#        print 'ispList len:' + str(len(ispList))
#        infos = rec.split('\t')
#        if len(infos) == 1 or len(rec.strip()) == 0 :
#            continue
#        print infos[0]
#        if infos[0].find(prevMainDomain) > -1 :
#            print 'same'
#            # 和主域名匹配，那么进行汇总操作
#            if cmp(infos[2], '移动') == 0:
#                cmccCount += 1
#            else :
#                noCmccCount += 1
#
#        else:
#            cmccCount = 0
#            noCmccCount = 0
#            ispList = ispList[i:]
#
#        i += 1
#    # 不一样，写前一个的记录，开始新的一个。
#    strTemp = prevMainDomain + '\t' + str(cmccCount) + '\t' + str(noCmccCount) + '\t' + str(cmccCount+noCmccCount)
#    print strTemp
#    summaryFile.write(strTemp + '\n')
            






